package com.gxc.service;

import com.gxc.annotation.MyLogAnnotation;
import com.gxc.dao.jpa.UserRepository;
import com.gxc.entity.User;
import com.gxc.event.UserRegisterEvent;
import java.util.Date;
import javax.annotation.Resource;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Service;

/**
 * 实现注册事件发布功能.
 *
 * @author 宫新程
 * @since 2018/12/6 10:57 AM
 */
@Service
public class UserService {

  @Resource private ApplicationContext applicationContext;
  @Resource private UserRepository userRepository;

  /** 用户注册方法. */
  @MyLogAnnotation(name = "register")
  public Boolean register(User user) {
    user.setLoginDate(new Date());
    user.setRole(1);
    user.setAge(20);
    userRepository.save(user);
    // 所以必须 UserRegisterEvent 重写构造方法.
    applicationContext.publishEvent(new UserRegisterEvent(this, user));
    return true;
  }
}
